package com.ic.shirodemoauth;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@SpringBootApplication
public class ShiroDemoAuthApplication {
	private static DefaultSecurityManager securityManager;

	public static void main(String[] args) {
		SpringApplication.run(ShiroDemoAuthApplication.class, args);

		securityManager = new DefaultSecurityManager();

		IniRealm iniRealm = new IniRealm("classpath:shiro.ini");

		securityManager.setRealm(iniRealm);

	}

	@GetMapping("/")
	@ResponseBody
    public String index() {
       return "index";
    }

    @RequestMapping( value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String login(HttpServletRequest req, RedirectAttributes attr) {
    	Subject subject = SecurityUtils.getSubject();

        if(!subject.isAuthenticated()) {
          UsernamePasswordToken token = new UsernamePasswordToken(
            "user", "password", true);
          try {
            subject.login(token);
          } catch (AuthenticationException ae) {
              ae.printStackTrace();
              attr.addFlashAttribute("error", "Invalid Credentials");
              return "redirect:/login";
          }
        }

        return "You are now logged in";
    }


    @GetMapping("/secure")
    @ResponseBody
    public String secure(ModelMap modelMap) {

		/*
		 * Subject currentUser = SecurityUtils.getSubject(); String role = "",
		 * permission = "";
		 * 
		 * if(currentUser.hasRole("admin")) { role = role + "You are an Admin"; } else
		 * if(currentUser.hasRole("editor")) { role = role + "You are an Editor"; } else
		 * if(currentUser.hasRole("author")) { role = role + "You are an Author"; }
		 * 
		 * if(currentUser.isPermitted("articles:compose")) { permission = permission +
		 * "You can compose an article, "; } else { permission = permission +
		 * "You are not permitted to compose an article!, "; }
		 * 
		 * if(currentUser.isPermitted("articles:save")) { permission = permission +
		 * "You can save articles, "; } else { permission = permission +
		 * "\nYou can not save articles, "; }
		 * 
		 * if(currentUser.isPermitted("articles:publish")) { permission = permission +
		 * "\nYou can publish articles"; } else { permission = permission +
		 * "\nYou can not publish articles"; }
		 * 
		 * System.out.println("username " + currentUser.getPrincipal());
		 * System.out.println("permission " + permission); System.out.println("role " +
		 * role);
		 */

        return "You now viewing the secured page";
    }


    @RequestMapping("/logout")
    public String logout() {
       Subject subject = SecurityUtils.getSubject();
       subject.logout();
       return "You are now logged out";
    }

}
